﻿using Newtonsoft.Json;
using Omabar.BLL;
using Omabar.Common;
using Omabar.Model;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.SessionState;

namespace Omabar.CMS.Handler
{
    /// <summary>
    /// CustomizedTravelAdd 的摘要说明
    /// </summary>
    public class CustomizedTravelAdd : IHttpHandler, IRequiresSessionState
    {
        public void ProcessRequest(HttpContext context)
        {
            RESP_Info info = new RESP_Info();
            try
            {
                context.Response.ContentType = "text/plain";

                string DESTINATION = context.Request["DESTINATION"];
                string STARTCITY = context.Request["STARTCITY"];
                string TRAVELDAYS = context.Request["TRAVELDAYS"];
                string ADULT = context.Request["ADULT"];
                string CHILD = context.Request["CHILD"];
                string BUDGET = context.Request["BUDGET"];
                string STARTDATE = context.Request["STARTDATE"];
                string THEMES = context.Request["THEMES"];
                string HOBBYS = context.Request["HOBBYS"];
                string ADDITIONAL = context.Request["ADDITIONAL"];
                string USERNAME = context.Request["USERNAME"];
                string USERID = context.Request["USERID"];
                string TELEPHONE = context.Request["TELEPHONE"];
                string EMAIL = context.Request["EMAIL"];

                CUSTOMIZEDTRAVEL ctravel = new CUSTOMIZEDTRAVEL()
                {
                    CT_ID = Util.GetUID(),
                    DESTINATION = DESTINATION,
                    STARTCITY = STARTCITY,
                    TRAVELDAYS = short.Parse(string.IsNullOrEmpty(TRAVELDAYS) ? "1" : TRAVELDAYS),
                    ADULT = short.Parse(string.IsNullOrEmpty(ADULT) ? "1" : ADULT),
                    CHILD = short.Parse(string.IsNullOrEmpty(CHILD) ? "0" : CHILD),
                    BUDGET = short.Parse(string.IsNullOrEmpty(BUDGET) ? "0" : BUDGET),
                    STARTDATE = DateTime.Parse(string.IsNullOrEmpty(STARTDATE) ? DateTime.Now.ToShortDateString() : STARTDATE),
                    THEMES = THEMES,
                    HOBBYS = HOBBYS,
                    ADDITIONAL = ADDITIONAL,
                    USERNAME = USERNAME,
                    USERID = string.IsNullOrEmpty(USERID) ? TELEPHONE : USERID,
                    TELEPHONE = TELEPHONE,
                    EMAIL = EMAIL
                };

                CustomizedTravel_BLL bll = new CustomizedTravel_BLL();
                info = bll.Add(ctravel);
            }
            catch (Exception ex)
            {
                Tom.Net.Log.LogManager.Error("添加定制之旅失败", ex);
                info.Success = false;
                info.Title = "错误";
                info.Tip = "添加定制之旅失败";
            }

            bool isMobile = string.IsNullOrEmpty(context.Request["isMobile"]) ? false : true;
            if (!isMobile)
                context.Response.Write(JsonConvert.SerializeObject(info));
            else
                context.Response.Redirect("../Mobile/PersonalTailor.aspx");
        }


        public bool IsReusable
        {
            get
            {
                return false;
            }
        }
    }
}